﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using weikebuluo.DAL;
using weikebuluo.Model;
using weikebuluo.Common;

namespace weikebuluo.wxgzpt.Customer.GuestBook
{
    public partial class GuestbookList : weikeweixin.web.admin.cs.adminbase
    {
        C_Guestbook_DAL cGuestbookDAL = new C_Guestbook_DAL();
        Model.C_Guestbook_Model GuestbookModel = new Model.C_Guestbook_Model();

        C_BlackList_DAL cBlackListDAL = new C_BlackList_DAL();
        C_BlackList_Model cBlackListModel = new C_BlackList_Model();
        JsonOperate JsonOperate = new JsonOperate();

        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Request.QueryString["action"])
            {
                case "bindGuestbookList":
                    GetGuestbookList();
                    break;
                case "del":
                    DelList();
                    break;
                case "blacklist":
                    InBlackList();
                    break;
                case "Audit":
                    GetAudit();
                    break;
            }

        }

        #region 查询留言的会员列表
        /// <summary>
        ///查询留言的会员列表
        /// </summary>
        public void GetGuestbookList()
        {


            string where = "User_ID=" + user_id + " and Guestbook_Stutas between 0 and 2 ";
            int Page = int.Parse(Request.Form["page"]);
            int RecordCount = cGuestbookDAL.GetRecordCount(where);

            DataTable dtGuestbook = cGuestbookDAL.GetListByPage(where, "Guestbook_Layer desc", ((Page - 1) * 15) + 1, (Page - 1) * 15 + 15).Tables[0];
            Response.Write(JsonOperate.GetDataGridJson(dtGuestbook, RecordCount));
            Response.End();
        }
        #endregion

        /// <summary>
        /// 删除留言
        /// </summary>
        public void DelList()
        {
            bool bl = cGuestbookDAL.Delete(int.Parse(Request.QueryString["Id"].Trim()));
        }

        /// <summary>
        /// 将会员加入黑名单
        /// </summary>
        public void InBlackList()
        {
            DataTable dtcBlackList = cBlackListDAL.GetList("BlackList_WeChatID ='" + Request.QueryString["Guestbook_WeChatID"].Trim() + "'").Tables[0];

            if (dtcBlackList.Rows.Count == 0)
            {
                cBlackListModel.BlackList_WeChatID = Request.QueryString["Guestbook_WeChatID"].Trim();
                cBlackListModel.BlackList_Time = DateTime.Now;
                cBlackListModel.User_ID = int.Parse(user_id);
                cBlackListDAL.Add(cBlackListModel);
            }
            int count = cGuestbookDAL.GetRecordCount("Guestbook_WeChatID = '" + Request.QueryString["Guestbook_WeChatID"].Trim() + "'");
            DataTable dtGuestbook;
            //将拉入黑名单的会员的所有留言拉黑
            for (int i = 0; i < count; i++) 
            {
                dtGuestbook = cGuestbookDAL.GetList("Guestbook_WeChatID = '" + Request.QueryString["Guestbook_WeChatID"].Trim() + "'").Tables[0];
                GuestbookModel = cGuestbookDAL.GetModel(int.Parse(dtGuestbook.Rows[i]["Guestbook_ID"].ToString()));
                GuestbookModel.Guestbook_Stutas = 3;
                cGuestbookDAL.Update(GuestbookModel);
            }
        }


        /// <summary>
        /// 审核会员留言
        /// </summary>
        public void GetAudit()
        {
            GuestbookModel = cGuestbookDAL.GetModel(int.Parse(Request.Form["id"]));
            if (Request.Form["Audit"].Equals("1"))
            {
                GuestbookModel.Guestbook_Stutas = 0;
            }
            else
            {
                GuestbookModel.Guestbook_Stutas = 1;
            }
            GuestbookModel.Guestbook_AuditTime = DateTime.Now;
            bool re = cGuestbookDAL.Update(GuestbookModel);
            if (re)
            {
                Response.Write("True");
            }
            else {
                Response.Write("false");
            }
            Response.End();
        }

    }
}
